#!/usr/bin/env python3
"""
快速更新 Changelog 的简化脚本
"""

import os
import sys
import subprocess
from pathlib import Path

# 添加脚本目录到路径
script_dir = Path(__file__).parent
sys.path.append(str(script_dir))

from changelog_generator import ChangelogGenerator


def quick_update():
    """快速更新 changelog"""
    print("🚀 快速更新 Changelog...")
    
    # 检查是否在 git 仓库中
    if not Path('.git').exists():
        print("❌ 当前目录不是 git 仓库")
        return False
    
    generator = ChangelogGenerator()
    
    # 获取最近的提交
    commits = generator.get_commits_since_tag()
    
    if not commits:
        print("❌ 没有新的提交需要记录")
        return False
    
    print(f"📝 找到 {len(commits)} 个新提交")
    
    # 分类提交
    categorized = generator.categorize_commits(commits)
    
    # 显示将要添加的内容
    print("\n📋 将添加以下内容到 Changelog:")
    has_changes = False
    for category, commits_in_category in categorized.items():
        if commits_in_category:
            has_changes = True
            print(f"\n{category}:")
            for commit in commits_in_category:
                print(f"  - {commit['description']}")
    
    if not has_changes:
        print("❌ 没有有效的变更内容")
        return False
    
    # 确认更新
    confirm = input(f"\n✅ 确认更新 Changelog? (y/N): ")
    if confirm.lower() != 'y':
        print("❌ 取消更新")
        return False
    
    # 计算新版本
    current_version = generator.get_latest_version()
    new_version = generator.increment_version(current_version, 'patch')
    
    # 更新 changelog
    generator.update_changelog(new_version, categorized)
    
    print(f"✅ Changelog 已更新到版本 {new_version}")
    
    # 询问是否提交更改
    commit_changes = input("🔧 是否提交 Changelog 更改? (y/N): ")
    if commit_changes.lower() == 'y':
        subprocess.run(['git', 'add', 'CHANGELOG.md'])
        subprocess.run(['git', 'commit', '-m', f'docs: update changelog to v{new_version}'])
        print("✅ Changelog 更改已提交")
    
    return True


if __name__ == '__main__':
    quick_update()